package com.apes.autoparts.parts.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.autoparts.parts.model.JsRequestLogging;
import com.apes.autoparts.parts.repository.JsRequestLoggingRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.rop.converter.DateUtils;
import com.apes.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Title: ZhaoPeiPackageService
 * @Author fj
 * @Package com.apes.autoparts.parts.service
 * @Date 2023/6/27 17:01
 * @description: 兆配接口服务封装
 */
@Service
public class ZhaoPeiPackageService {

    @Autowired
    private QuerySubGroupService querySubGroupService;

    @Autowired
    private QueryPartsService queryPartsService;

    @Autowired
    private QueryMainGroupService queryMainGroupService;

    @Autowired
    private JsRequestLoggingRepository jsRequestLoggingRepository;

    @Autowired
    private QueryVinService queryVinService;

    /***
     * 1、vin 解析
     * @param request {"vin":"LFMKN5BF5L3062308","selectorId":"honda"}
     * @return
     */
    public JSONObject checkVinData(SimpleRequest request) {
        JSONObject result = queryVinService.checkVinData(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 4、查询主组
     * @param request {"vin":"LFMKN5BF5L3062308","id":"251416","brandCode":"toyota"}
     * @return
     */
    public JSONArray parseMainGroup(SimpleRequest request){
        JSONArray mainGroup = queryMainGroupService.queryMainGroup(request);
        saveLog(request,mainGroup.toString());
        return mainGroup;
    }

    /***
     * 5、通过主组查询子组数据
     * @param request {"vin":"LFMKN5BF5L3062308","id":"1","brandCode":"toyota","selectorId":"2384"}
     * @return
     */
    public JSONArray parseSubGroup(SimpleRequest request) {
        JSONArray result = querySubGroupService.querySubGroup(request);
        saveLog(request,result.toString());
        return result;
    }

    /***
     * 6、通过子组查询配件数据
     * @param request {"vin":"LFMKN5BF5L3062308","id":"251356","brandCode":"toyota","selectorId":"2384","mainGroupId":"1"}
     * @return
     */
    public List<Object> parseParts(SimpleRequest request) {
        JSONArray result = queryPartsService.queryParts(request);
        saveLog(request,result.toString());
        return result;
    }

    /***
     * 7、通过子组查询热点图数据
     * @param request {"vin":"LFMKN5BF5L3062308","id":"251356","brandCode":"toyota","selectorId":"2384","mainGroupId":"1"}
     * @return
     */
    public JSONObject parseImgPoint(SimpleRequest request) {
        JSONObject result = querySubGroupService.queryImgPoint(request);
        saveLog(request,result.toString());
        return result;
    }

    /***
     * 10、模糊查询配件
     * @param request {"codes":"","keywords":"电","brandCode":"toyota"}
     * @return
     */
    public JSONObject fuzzyQueryOfParts(SimpleRequest request) {
        JSONObject result = queryPartsService.fuzzyQueryOfParts(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 11、配件筛选
     * @param request {"keywords":"电","brandCode":"toyota"}
     * @return
     */
    public JSONObject queryPartsSimple(SimpleRequest request) {
        JSONObject result = queryPartsService.queryPartsSimple(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 12、车辆配件模糊搜索
     * @param request {"vin":"LFMKN5BF5L3062308","brandCode":"toyota","selectorId":"2384","keywords":"电"}
     * @return
     */
    public JSONObject searchParts(SimpleRequest request) {
        JSONObject result = queryPartsService.searchParts(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 13、通过OE号获取品牌件信息
     * @param request {"partCode":"359740R020","pageIndex":"0","type":0,"supplierId":0,"manufacturerId":0}
     * @return
     */
    public JSONObject queryBrandParts(SimpleRequest request) {
        JSONObject result = queryPartsService.queryBrandParts(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 16、通过vin和keyworks查询配件
     * @param request {"vin":"LFMKN5BF5L3062308","keywords":"电","isTerm":"false"}
     * @return
     */
    public JSONObject searchPartsByVinKeywords(SimpleRequest request) {
        JSONObject result = queryPartsService.searchPartsByVinKeywords(request);
        saveLog(request,result.toString());
        return result;
    }

    /**
     * 日志保存
     * @param request 请求参数
     * @param response 响应结果
     */
    private void saveLog(SimpleRequest request, String response){
        JsRequestLogging logging = new JsRequestLogging();
        String beginTime = DateUtil.timeStampToStr(request.getRopRequestContext().getServiceBeginTime());
        String method = request.getRopRequestContext().getMethod();
        logging.setBeginTime(DateUtils.parseDate(beginTime,DateUtil.SHOW_DATETIME_MILLI_FORMAT));
        logging.setEndTime(new Date());
        logging.setName(method);
        logging.setRequest(request.getJO().toString());
        logging.setResponse(response);
        logging.setTime(logging.getEndTime().getTime() - logging.getBeginTime().getTime());
        jsRequestLoggingRepository.saveAndFlush(logging);
    }
}
